<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="test"></div>


    <!-- 引入react核心库 -->
    <script type="text/javascript" src="../js/react.development.js"></script>
    <script type="text/javascript" src="../js/react-dom.development.js"></script>
    <script type="text/javascript" src="../js/babel.min.js"></script>

    <script type="text/babel">  /*此处一定要写babel*/

      class Count extends React.Component{

        constructor(props){
            console.log('Count - constructor')
            super(props)

              //初始化状态
            this.state={count:0}

        }
      
        add = ()=>{
            //获取原状态
            const {count} = this.state
            //更新状态
            this.setState({count:count+1})
        }

        //卸载组件按钮的回调
        death = ()=>{
            ReactDOM.unmountComponentAtNode(document.getElementById('test'))
        }


        //组件将要挂载的钩子
        componentWillMount(){
            console.log('Count -- componentWillMount')
        }

        //组件挂载完毕
        componentDidMount(){
            console.log('Count ---- componentDidMount')
        }

        //组件将要卸载的钩子
        componentWillUnmount(){
            console.log('Count ----- componentWillUnmount')
        }

        //控制组件是否更新的阀门 默认return true
        shouldComponentUpdate(){
            console.log("Count --- 组件是否允许修改状态")
            return true;
        }

        //更新完毕后的钩子
        componentDidUpdate(){
            console.log("Count --- componentDidUpdate")
        }
        render(){
            console.log('Count --- render')
              const {count} = this.state
              return(
                  <div>
                    <h2>当前求和为:{count}</h2>
                    <button onClick={this.add}>点我加1</button>
                    <button onClick={this.death}>卸载组件</button>
                    </div>
              )
          }
      }

      //渲染
      ReactDOM.render(<Count/>,document.getElementById('test'))
    </script>

    
</body>
</html>